//
//  ZYIndexDataViewModel.m
//  YiLin
//
//  Created by Zyark on 2018/7/13.
//  Copyright © 2018年 Zyark. All rights reserved.
//

#import "ZYIndexDataViewModel.h"
#import "AFNetWorkUtils.h"
#import "ZYHome.h"

@interface ZYIndexDataViewModel()
/**
 首页顶部数据
 */
@property(nonatomic,strong) RACCommand *dataCommand;
/**
 广告图片
 */
@property(nonatomic,strong) RACCommand *adCommand;
@end

@implementation ZYIndexDataViewModel


-(RACCommand *)dataCommand{
    if(!_dataCommand){
        @weakify(self);
        _dataCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
            @strongify(self);
            NSString *url = [NSString stringWithFormat:@"%@%@",ZYAPI_BASE_URL,ZYAPI_INDEX_HEADER_DATA_URL];
            NSDictionary *params = @{@"ssid":[ZYConfig loginToken],@"houseId":[NSNumber numberWithInteger:self.houseId]};
            return [[AFNetWorkUtils racGETWithURL:url params:params] map:^id(id value) {
                ZYHome *home = [ZYHome modelWithJSON:[value objectForKey:@"result"]];
                return home;
            }];
        }];
    }
    return _dataCommand;
}

-(RACCommand *)adCommand{
    if(!_adCommand){
        _adCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
            NSString *url = [NSString stringWithFormat:@"%@%@",ZYAPI_BASE_URL,ZYAPI_AD_URL];
            return [[AFNetWorkUtils racGETWithURL:url] map:^id(id value) {
                NSArray *result = [value objectForKey:@"result"];
                if(result && [result count] > 0){
                    NSDictionary *dict = [result firstObject];
                    return [dict objectForKey:@"imageUrl1"];
                }
                return @"";
            }];
        }];
    }
    return _adCommand;
}

@end
